﻿using cuyan.orm;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using System.Text;
namespace ORM
{

    public class TestName
    {
        public string Name { get; set; }
    }

    public class Student
    {
        public long Id { get; set; }
        public string Name { get; set; }
        public int Gender { get; set; }
        public bool IsGraduate { get; set; }
    }


    public class StudentModel
    {
        public long Id { get; set; }
        public string Name { get; set; }
        public int Gender { get; set; }
        public int IsGraduate { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            var _db = new CuyanClient()
            {
                ConnectionString = "Server=120.79.19.125;Initial Catalog=cuyan;User ID=cuyan;Password=123456"
            };

            //新增
            var colorTemp = Console.ForegroundColor;
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine($"======= 新增 =======");
            Console.ForegroundColor = colorTemp;
            if (true)
            {
                var result = _db.Add(new Student() { Gender = 1, Name = "张三", IsGraduate = false });
                if (result > 0)
                    Console.WriteLine($"受影响数据：{result} 条。（添加成功）");
                else
                    Console.WriteLine($"受影响数据：{result} 条。（添加失败）");
            }
            Console.WriteLine("\n");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine($"======= 查询 =======");
            Console.ForegroundColor = colorTemp;
            //查询
            if (true)
            {
                var students = _db.Query<Student>("SELECT * FROM[cuyan].[dbo].[Student]", null);
                if (students != null && students.Count > 0)
                {
                    Console.WriteLine($"查询到数据 {students.Count} 条");
                    Console.WriteLine($"展示第一条：名字：{students[0].Name}，是否毕业：{students[0].IsGraduate}");
                }
            }
            Console.WriteLine("\n");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine($"======= 修改更新 =======");
            Console.ForegroundColor = colorTemp;
            //修改更新
            if (true)
            {
                var students = _db.Query<Student>("SELECT * FROM[cuyan].[dbo].[Student]", null);
                if (students != null && students.Count > 0)
                {
                    //修改数据
                    students[0].IsGraduate = true;
                    //更新数据
                    var result = _db.Update(students[0]);
                    if (result > 0)
                        Console.WriteLine($"受影响数据：{result} 条。（修改更新成功）");
                    else
                        Console.WriteLine($"受影响数据：{result} 条。（修改更新失败）");
                }
            }
            Console.WriteLine("\n");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine($"======= 删除 =======");
            Console.ForegroundColor = colorTemp;
            //删除
            if (true)
            {
                var students = _db.Query<Student>("SELECT * FROM[cuyan].[dbo].[Student]", null);
                if (students != null && students.Count > 0)
                {
                    //删除(根据主键删除)
                    var result = _db.Delete(students[0]);
                    if (result > 0)
                        Console.WriteLine($"受影响数据：{result} 条。（删除成功）");
                    else
                        Console.WriteLine($"受影响数据：{result} 条。（删除失败）");
                }
            }

            Console.WriteLine("\n");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine($"======= 映射对象 Mapping =======");
            Console.ForegroundColor = colorTemp;
            //映射对象 Mapping
            if (true)
            {
                var students = _db.Query<Student>("SELECT * FROM[cuyan].[dbo].[Student]", null);
                var studentModels = Mapper.MapList<Student, StudentModel>(students);

                if (studentModels != null && studentModels.Count > 0)
                {
                    Console.WriteLine($"查询到数据 {students.Count} 条，转换数据 {studentModels.Count} 条");
                    Console.WriteLine($"展示第一条：名字：{studentModels[0].Name}，是否毕业：{studentModels[0].IsGraduate}");
                }
            }

            Console.ReadKey();
        }
    }
}
